<!DOCTYPE HTML>
<html lang="zh-CN">
<head>
    

<head><meta name="generator" content="Hexo 3.8.0">
    <meta charset="utf-8">
    <meta name="keywords" content="小波iOS11中级篇笔记, 炫杉">
    <meta name="description" content="中级篇啦🤣开心点咯。虽然最近有点低落，就当发泄了。

小知识点
&amp;quot;&amp;quot;&amp;quot;三引号对&amp;quot;&amp;quot;&amp;quot;可以保留复制来的文本换行
图片显示方式：
Scale to fill 拉伸 即拉到和框框一样的">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta name="renderer" content="webkit|ie-stand|ie-comp">
    <meta name="mobile-web-app-capable" content="yes">
    <meta name="format-detection" content="telephone=no">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="apple-mobile-web-app-status-bar-style" content="black-translucent">
    <title>小波iOS11中级篇笔记 | 炫杉</title>
    <link rel="icon" type="image/png" href="/favicon.png">

    <link rel="stylesheet" type="text/css" href="/libs/awesome/css/font-awesome.min.css">
    <link rel="stylesheet" type="text/css" href="/libs/materialize/css/materialize.min.css">
    <link rel="stylesheet" type="text/css" href="/libs/aos/aos.css">
    <link rel="stylesheet" type="text/css" href="/libs/animate/animate.min.css">
    <link rel="stylesheet" type="text/css" href="/libs/lightGallery/css/lightgallery.min.css">
    <link rel="stylesheet" type="text/css" href="/css/matery.css">
    <link rel="stylesheet" type="text/css" href="/css/my.css">

    <script src="/libs/jquery/jquery-2.2.0.min.js"></script>
<link rel="stylesheet" href="/css/prism-tomorrow.css" type="text/css">
<link rel="stylesheet" href="/css/prism-line-numbers.css" type="text/css"></head>

</head>

<body>

<header class="navbar-fixed">
    <nav id="headNav" class="bg-color nav-transparent">
        <div id="navContainer" class="container">
            <div class="nav-wrapper">
                <div class="brand-logo">
                    <a href="/" class="waves-effect waves-light">
                        
                        <img src="/medias/logo.png" class="logo-img hide-on-small-only">
                        
                        <span class="logo-span">炫杉</span>
                    </a>
                </div>
                <a href="#" data-activates="mobile-nav" class="button-collapse"><i class="fa fa-navicon"></i></a>
<ul class="right">
    
    <li class="hide-on-med-and-down">
        <a href="/" class="waves-effect waves-light">
            
            <i class="fa fa-home"></i>
            
            <span>Index</span>
        </a>
    </li>
    
    <li class="hide-on-med-and-down">
        <a href="/tags" class="waves-effect waves-light">
            
            <i class="fa fa-tags"></i>
            
            <span>Tags</span>
        </a>
    </li>
    
    <li class="hide-on-med-and-down">
        <a href="/categories" class="waves-effect waves-light">
            
            <i class="fa fa-bookmark"></i>
            
            <span>Categories</span>
        </a>
    </li>
    
    <li class="hide-on-med-and-down">
        <a href="/archives" class="waves-effect waves-light">
            
            <i class="fa fa-archive"></i>
            
            <span>Archives</span>
        </a>
    </li>
    
    <li class="hide-on-med-and-down">
        <a href="/about" class="waves-effect waves-light">
            
            <i class="fa fa-user-circle-o"></i>
            
            <span>About</span>
        </a>
    </li>
    
    <li class="hide-on-med-and-down">
        <a href="https://oo1.win" class="waves-effect waves-light">
            
            <i class="fa fa-star"></i>
            
            <span>Bookmark</span>
        </a>
    </li>
    
    <li>
        <a id="toggleSearch" class="waves-effect waves-light">
            <i id="searchIcon" class="mdi-action-search"></i>
        </a>
    </li>

</ul>

<div class="side-nav" id="mobile-nav">

    <div class="mobile-head bg-color">
        
        <img src="/medias/logo.png" class="logo-img circle responsive-img">
        
        <div class="logo-name">炫杉</div>
        <div class="logo-desc">
            
            提着鸟笼的老头站在一旁拍我肩膀 我猜他想的应该和我一样
            
        </div>
    </div>

    <ul class="menu-list mobile-menu-list">
        
        <li>
            <a href="/" class="waves-effect waves-light">
                
                <i class="fa fa-fw fa-home"></i>
                
                Index
            </a>
        </li>
        
        <li>
            <a href="/tags" class="waves-effect waves-light">
                
                <i class="fa fa-fw fa-tags"></i>
                
                Tags
            </a>
        </li>
        
        <li>
            <a href="/categories" class="waves-effect waves-light">
                
                <i class="fa fa-fw fa-bookmark"></i>
                
                Categories
            </a>
        </li>
        
        <li>
            <a href="/archives" class="waves-effect waves-light">
                
                <i class="fa fa-fw fa-archive"></i>
                
                Archives
            </a>
        </li>
        
        <li>
            <a href="/about" class="waves-effect waves-light">
                
                <i class="fa fa-fw fa-user-circle-o"></i>
                
                About
            </a>
        </li>
        
        <li>
            <a href="https://oo1.win" class="waves-effect waves-light">
                
                <i class="fa fa-fw fa-star"></i>
                
                Bookmark
            </a>
        </li>
        
        
    </ul>

    <div class="social-link"><a href="https://github.com/ixsim" class="tooltipped" target="_blank" data-tooltip="访问我的GitHub" data-position="top" data-delay="50">
    <i class="fa fa-github"></i>
</a>

<a href="mailto:chenjiayin1990@163.com" class="tooltipped" target="_blank" data-tooltip="邮件:chinalixs@qq.com" data-position="top" data-delay="50">
    <i class="fa fa-envelope-open"></i>
</a>

<a href="http://wpa.qq.com/msgrd?v=3&uin=6956820&site=qq&menu=yes" class="tooltipped" data-tooltip="QQ: 6956820" data-position="top" data-delay="50">
    <i class="fa fa-qq"></i>
</a>
</div>
</div>

            </div>
        </div>

        
    </nav>
</header>



<div class="bg-cover post-cover" style="background-image: url('/medias/featureimages/17.jpg')">
    <div class="container">
        <div class="row">
            <div class="col s12 m12 l12">
                <div class="brand">
                    <div class="description center-align post-title">
                        小波iOS11中级篇笔记
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>



<main class="post-container content">

    
    <link rel="stylesheet" href="/libs/tocbot/tocbot.css">
<style>
    #articleContent h1,
    #articleContent h2,
    #articleContent h3,
    #articleContent h4,
    #articleContent h5,
    #articleContent h6 {
        padding-top: 76px;
        margin-top: -76px;
    }

    #articleContent h1 {
        line-height: 3.5rem;
    }

    #articleContent h2 {
        line-height: 3.2rem;
    }

    #articleContent h3 {
        line-height: 2.8rem;
    }

    #articleContent h4 {
        line-height: 2.5rem;
    }

    #articleContent h5 {
        line-height: 2.2rem;
    }

    #articleContent h6 {
        line-height: 1.9rem;
    }

    #articleContent :focus {
        outline: none;
    }

    .toc-fixed {
        position: fixed;
        top: 64px;
    }

    .toc-widget {
        padding-left: 20px;
    }

    .toc-widget .toc-title {
        margin: 35px 0 15px 0;
        padding-left: 17px;
        font-size: 1.5rem;
        font-weight: bold;
        line-height: 1.5rem;
    }

    .toc-widget ol {
        padding: 0;
        list-style: none;
    }

    #toc-content ol {
        padding-left: 10px;
    }

    #toc-content ol li {
        padding-left: 10px;
    }

    #toc-content .toc-link:hover {
        color: #42b983;
        font-weight: 700;
        text-decoration: underline;
    }

    #toc-content .toc-link::before {
        background-color: transparent;
        max-height: 25px;
    }

    #toc-content .is-active-link {
        color: #42b983;
    }

    #toc-content .is-active-link::before {
        background-color: #42b983;
    }
</style>
<div class="row">
    <div class="col s12 m12 l9">
        <!-- 文章内容详情 -->
<div id="artDetail">
    <div class="card">
        <div class="card-content article-info">
            
            <div class="article-tag">
                
                <a href="/tags/Note/" target="_blank">
                    <span class="chip bg-color">Note</span>
                </a>
                
                <a href="/tags/Swfit/" target="_blank">
                    <span class="chip bg-color">Swfit</span>
                </a>
                
            </div>
            
            <div class="post-info">
                
                <span class="post-cate">
                    <i class="fa fa-bookmark fa-fw icon-category"></i>
                    
                    <a href="/categories/iOS/" class="post-category" target="_blank">
                        iOS
                    </a>
                    
                </span>
                

                <span class="post-date">
                    <i class="fa fa-clock-o fa-fw"></i>2018-04-07
                </span>
            </div>
        </div>
        <hr>
        <div class="card-content article-card-content">
            <div id="articleContent">
                <p>中级篇啦🤣开心点咯。虽然最近有点低落，就当发泄了。</p>
<a id="more"></a>
<h1 id="小知识点"><a href="#小知识点" class="headerlink" title="小知识点"></a>小知识点</h1><ul>
<li><code>&quot;&quot;&quot;</code>三引号对<code>&quot;&quot;&quot;</code>可以保留复制来的文本换行</li>
<li>图片显示方式：<ul>
<li>Scale to fill 拉伸 即拉到和框框一样的比例/大小</li>
<li>Aspect fit 居中 原图比例不变，但会有白边</li>
<li>Aspect fill 平铺 ，通常配合下面的裁边Clip to bounds使用</li>
</ul>
</li>
<li>属性line设为0. 即自动控制行数</li>
<li>内容扩展优先级 Content Hugging Priority 大了就低</li>
<li>Class 中的属性必须有值。如果你不想给值，可以加个 ！ 就是说它一定有值。</li>
<li>Btn.setTitle(“改标题”)</li>
<li>拖新文件到工程里记得选上<code>Copy item if needed</code>，不然只拖了个快捷方式</li>
<li>Didload只会执行一次</li>
<li>在整个Controller的属性中，选高度–》Freedom–》可以随意设置。方便查看。</li>
<li>转String可以不用<code>String(a)</code>用<code>a.description</code></li>
</ul>
<h1 id="自定义导航条"><a href="#自定义导航条" class="headerlink" title="自定义导航条"></a>自定义导航条</h1><h2 id="颜色（直接改回报错，是Xcode的Bug。只好用代码。）："><a href="#颜色（直接改回报错，是Xcode的Bug。只好用代码。）：" class="headerlink" title="颜色（直接改回报错，是Xcode的Bug。只好用代码。）："></a>颜色（直接改回报错，是Xcode的Bug。只好用代码。）：</h2><pre class="line-numbers language-swift"><code class="language-swift">navigationController<span class="token operator">?</span><span class="token punctuation">.</span>navigationBar<span class="token punctuation">.</span>largeTitleTextAttributes <span class="token operator">=</span> <span class="token punctuation">[</span>
                <span class="token builtin">NSAttributedStringKey</span><span class="token punctuation">.</span>foregroundColor <span class="token punctuation">:</span> <span class="token function">UIColor</span><span class="token punctuation">(</span>named<span class="token punctuation">:</span> <span class="token string">"Theme123"</span><span class="token punctuation">)</span>
            <span class="token punctuation">]</span>
            <span class="token comment" spellcheck="true">//Theme123是自定义的Color Set名</span>
<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span></span></code></pre>
<h2 id="背景透明（用一个空图片）"><a href="#背景透明（用一个空图片）" class="headerlink" title="背景透明（用一个空图片）"></a>背景透明（用一个空图片）</h2><pre class="line-numbers language-swift"><code class="language-swift">navigationController<span class="token operator">?</span><span class="token punctuation">.</span>navigationBar<span class="token punctuation">.</span><span class="token function">setBackgroundImage</span><span class="token punctuation">(</span><span class="token function">UIImage</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">,</span> <span class="token keyword">for</span><span class="token punctuation">:</span> <span class="token punctuation">.</span><span class="token keyword">default</span><span class="token punctuation">)</span>
navigationController<span class="token operator">?</span><span class="token punctuation">.</span>navigationBar<span class="token punctuation">.</span>shadowImage <span class="token operator">=</span> <span class="token function">UIImage</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span></span></code></pre>
<h2 id="隐藏返回标题"><a href="#隐藏返回标题" class="headerlink" title="隐藏返回标题"></a>隐藏返回标题</h2><p><img src="http://p66eruxmw.bkt.clouddn.com/15231886419894.jpg" alt=""><br>点标题。把Back button改成空格就好啦。</p>
<h2 id="自定义状态栏字体颜色"><a href="#自定义状态栏字体颜色" class="headerlink" title="自定义状态栏字体颜色"></a>自定义状态栏字体颜色</h2><pre class="line-numbers language-swift"><code class="language-swift">    <span class="token keyword">override</span> <span class="token keyword">var</span> preferredStatusBarStyle<span class="token punctuation">:</span> <span class="token builtin">UIStatusBarStyle</span><span class="token punctuation">{</span>
        <span class="token keyword">return</span> <span class="token punctuation">.</span>lightContent
    <span class="token punctuation">}</span>
<span class="token comment" spellcheck="true">//写了上面的发现还不行。因为有全局导航条样式</span>

<span class="token comment" spellcheck="true">//新建一个swift文件： naviExt.swfit 写入以下内容</span>
<span class="token keyword">import</span> <span class="token builtin">UIKit</span>
<span class="token comment" spellcheck="true">//扩展导航条</span>
<span class="token keyword">extension</span> <span class="token builtin">UINavigationController</span><span class="token punctuation">{</span>
    open <span class="token keyword">override</span> <span class="token keyword">var</span> childViewControllerForStatusBarStyle<span class="token punctuation">:</span> <span class="token builtin">UIViewController</span><span class="token operator">?</span><span class="token punctuation">{</span>
        <span class="token keyword">return</span> topViewController
    <span class="token punctuation">}</span>
<span class="token punctuation">}</span>
<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>
<p>没有导航条怎么改状态栏颜色：<br>在AppDelegate.swift文件中，插入到application函数一行：<br><code>UIApplication.shared.statusBarStyle = .lightContent</code></p>
<h1 id="动画"><a href="#动画" class="headerlink" title="动画"></a>动画</h1><h2 id="视差"><a href="#视差" class="headerlink" title="视差"></a>视差</h2><p>新建一个UIView变量<code>var headerView : UIView!</code>来替换原生的<code>tableView.tableHeaderView</code><br>代码如下</p>
<pre class="line-numbers language-swift"><code class="language-swift">        headerView <span class="token operator">=</span> tableView<span class="token punctuation">.</span>tableHeaderView<span class="token comment" spellcheck="true">//取出HeaderView</span>
        tableView<span class="token punctuation">.</span>tableHeaderView <span class="token operator">=</span> <span class="token constant">nil</span> 
        tableView<span class="token punctuation">.</span><span class="token function">addSubview</span><span class="token punctuation">(</span>headerView<span class="token punctuation">)</span>

        tableView<span class="token punctuation">.</span>contentInset <span class="token operator">=</span> <span class="token function">UIEdgeInsets</span><span class="token punctuation">(</span>top<span class="token punctuation">:</span> <span class="token number">300</span><span class="token punctuation">,</span> <span class="token keyword">left</span><span class="token punctuation">:</span> <span class="token number">0</span><span class="token punctuation">,</span> bottom<span class="token punctuation">:</span> <span class="token number">0</span><span class="token punctuation">,</span> <span class="token keyword">right</span><span class="token punctuation">:</span> <span class="token number">0</span><span class="token punctuation">)</span>
<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span></span></code></pre>
<p>这时候整个Cell会下移300像素（可以通过输出<code>scrollView.contentOffset.y</code>的值看到是 -300），需要把headerView的位置上移300个像素。<br>代码如下</p>
<pre class="line-numbers language-swift"><code class="language-swift"><span class="token keyword">override</span> <span class="token keyword">func</span> <span class="token function">scrollViewDidScroll</span><span class="token punctuation">(</span><span class="token number">_</span> scrollView<span class="token punctuation">:</span> <span class="token builtin">UIScrollView</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token keyword">let</span> offsetY <span class="token operator">=</span> scrollView<span class="token punctuation">.</span>contentOffset<span class="token punctuation">.</span>y
        headerView<span class="token punctuation">.</span>frame <span class="token operator">=</span> <span class="token function">CGRect</span><span class="token punctuation">(</span>x<span class="token punctuation">:</span> <span class="token number">0</span><span class="token punctuation">,</span> y<span class="token punctuation">:</span> offsetY<span class="token punctuation">,</span> width<span class="token punctuation">:</span> scrollView<span class="token punctuation">.</span>bounds<span class="token punctuation">.</span>width<span class="token punctuation">,</span> height<span class="token punctuation">:</span> <span class="token operator">-</span>offsetY<span class="token punctuation">)</span>
    <span class="token punctuation">}</span>
<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span></span></code></pre>
<h2 id="模糊层"><a href="#模糊层" class="headerlink" title="模糊层"></a>模糊层</h2><ol>
<li>新建模糊层的 ViewController 。背景选成透明（Clear Color）</li>
<li>右键拖到新的 View Controller，选 Present Modally</li>
<li>转场效果中选 Over Current Context</li>
</ol>
<p>点击退场</p>
<p>拖一个手势到 <strong>View层</strong> 注意拖得不合适可能点击没效果。可以调试下。</p>
<p>然后拖着手势建一个IBAction：</p>
<pre class="line-numbers language-swift"><code class="language-swift"><span class="token atrule">@IBAction</span> <span class="token keyword">func</span> <span class="token function">tapBackground</span><span class="token punctuation">(</span><span class="token number">_</span> sender<span class="token punctuation">:</span> <span class="token builtin">Any</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token keyword">self</span><span class="token punctuation">.</span><span class="token function">dismiss</span><span class="token punctuation">(</span>animated<span class="token punctuation">:</span> <span class="token boolean">true</span><span class="token punctuation">)</span>
    <span class="token punctuation">}</span>
<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span></span></code></pre>
<h2 id="入场动画"><a href="#入场动画" class="headerlink" title="入场动画"></a>入场动画</h2><p>先在 didLoaded 时把它放到800里之外<br><code>stackView.transform = .init(translationX: 800, y: 0)</code></p>
<p>再在 didApeal时 让它飞进来</p>
<pre class="line-numbers language-swift"><code class="language-swift"><span class="token keyword">override</span> <span class="token keyword">func</span> <span class="token function">viewDidAppear</span><span class="token punctuation">(</span><span class="token number">_</span> animated<span class="token punctuation">:</span> <span class="token builtin">Bool</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token keyword">let</span>  animator <span class="token operator">=</span> <span class="token function">UIViewPropertyAnimator</span><span class="token punctuation">(</span>duration<span class="token punctuation">:</span> <span class="token number">3</span><span class="token punctuation">,</span> curve<span class="token punctuation">:</span> <span class="token punctuation">.</span>easeIn<span class="token punctuation">)</span><span class="token punctuation">{</span>
            <span class="token keyword">self</span><span class="token punctuation">.</span>stackView<span class="token punctuation">.</span>transform <span class="token operator">=</span> <span class="token punctuation">.</span>identity
        <span class="token punctuation">}</span>
        animator<span class="token punctuation">.</span><span class="token function">startAnimation</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
    <span class="token punctuation">}</span>
<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>
<p>进阶版<br>按钮组在这里拖按钮。<br><img src="http://p66eruxmw.bkt.clouddn.com/15233413710890.jpg" alt="按钮组"></p>
<p>然后把变量分别拖到各个按钮上，这些按钮就是一组的了。</p>
<pre class="line-numbers language-swift"><code class="language-swift"><span class="token keyword">override</span> <span class="token keyword">func</span> <span class="token function">viewDidLoad</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token keyword">super</span><span class="token punctuation">.</span><span class="token function">viewDidLoad</span><span class="token punctuation">(</span><span class="token punctuation">)</span>

        <span class="token keyword">let</span> startPostion <span class="token operator">=</span> <span class="token function">CGAffineTransform</span><span class="token punctuation">(</span>translationX<span class="token punctuation">:</span> <span class="token number">500</span><span class="token punctuation">,</span> y<span class="token punctuation">:</span> <span class="token number">0</span><span class="token punctuation">)</span>

        <span class="token keyword">for</span> button <span class="token keyword">in</span> rateItemBtn<span class="token punctuation">{</span>
            button<span class="token punctuation">.</span>transform <span class="token operator">=</span> startPostion
            button<span class="token punctuation">.</span>alpha <span class="token operator">=</span> <span class="token number">0</span> <span class="token comment" spellcheck="true">//把他们都设透明</span>
        <span class="token punctuation">}</span> 
    <span class="token punctuation">}</span>

    <span class="token keyword">override</span> <span class="token keyword">func</span> <span class="token function">viewDidAppear</span><span class="token punctuation">(</span><span class="token number">_</span> animated<span class="token punctuation">:</span> <span class="token builtin">Bool</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
<span class="token function">UIViewPropertyAnimator</span><span class="token punctuation">(</span>duration<span class="token punctuation">:</span> <span class="token number">0.4</span><span class="token punctuation">,</span> dampingRatio<span class="token punctuation">:</span> <span class="token number">0.5</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
            <span class="token keyword">self</span><span class="token punctuation">.</span>rateItemBtn<span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">]</span><span class="token punctuation">.</span>transform <span class="token operator">=</span> <span class="token punctuation">.</span>identity
            <span class="token keyword">self</span><span class="token punctuation">.</span>rateItemBtn<span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">]</span><span class="token punctuation">.</span>alpha <span class="token operator">=</span> <span class="token number">1</span>
            <span class="token punctuation">}</span><span class="token punctuation">.</span><span class="token function">startAnimation</span><span class="token punctuation">(</span>afterDelay<span class="token punctuation">:</span> <span class="token number">0.1</span><span class="token punctuation">)</span><span class="token comment" spellcheck="true">//第一个按钮0.1秒后出现</span>
    <span class="token punctuation">}</span>
<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>
<h2 id="拖拽手势"><a href="#拖拽手势" class="headerlink" title="拖拽手势"></a>拖拽手势</h2><pre class="line-numbers language-swift"><code class="language-swift">    <span class="token atrule">@IBAction</span> <span class="token keyword">func</span> <span class="token function">dragStackView</span><span class="token punctuation">(</span><span class="token number">_</span> sender<span class="token punctuation">:</span> <span class="token builtin">UIPanGestureRecognizer</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token keyword">switch</span> sender<span class="token punctuation">.</span>state <span class="token punctuation">{</span>
        <span class="token keyword">case</span> <span class="token punctuation">.</span>changed<span class="token punctuation">:</span>
            <span class="token keyword">let</span> translate <span class="token operator">=</span> sender<span class="token punctuation">.</span><span class="token function">translation</span><span class="token punctuation">(</span><span class="token keyword">in</span><span class="token punctuation">:</span> view<span class="token punctuation">)</span>
            <span class="token keyword">let</span> position <span class="token operator">=</span> <span class="token function">CGAffineTransform</span><span class="token punctuation">(</span>translationX<span class="token punctuation">:</span> translate<span class="token punctuation">.</span>x<span class="token punctuation">,</span> y<span class="token punctuation">:</span> translate<span class="token punctuation">.</span>y<span class="token punctuation">)</span>
            <span class="token keyword">let</span> angle <span class="token operator">=</span> <span class="token function">sin</span><span class="token punctuation">(</span>translate<span class="token punctuation">.</span>x <span class="token operator">/</span> stackView<span class="token punctuation">.</span>frame<span class="token punctuation">.</span>width<span class="token punctuation">)</span>
            stackView<span class="token punctuation">.</span>transform <span class="token operator">=</span> position<span class="token punctuation">.</span><span class="token function">rotated</span><span class="token punctuation">(</span>by<span class="token punctuation">:</span> angle<span class="token punctuation">)</span>
        <span class="token keyword">case</span> <span class="token punctuation">.</span>ended<span class="token punctuation">:</span>
            <span class="token function">UIViewPropertyAnimator</span><span class="token punctuation">(</span>duration<span class="token punctuation">:</span> <span class="token number">0.4</span><span class="token punctuation">,</span> dampingRatio<span class="token punctuation">:</span> <span class="token number">0.5</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
                <span class="token keyword">self</span><span class="token punctuation">.</span>stackView<span class="token punctuation">.</span>transform <span class="token operator">=</span> <span class="token punctuation">.</span>identity
            <span class="token punctuation">}</span><span class="token punctuation">.</span><span class="token function">startAnimation</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
        <span class="token keyword">default</span><span class="token punctuation">:</span>
            <span class="token keyword">break</span>
        <span class="token punctuation">}</span>   
    <span class="token punctuation">}</span>   
<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>
<h2 id="反向转场"><a href="#反向转场" class="headerlink" title="反向转场"></a>反向转场</h2><p>先写一个转场函数：</p>
<pre><code>@IBAction func backToDetail(segue: UIStoryboardSegue){}
</code></pre><p><img src="http://p66eruxmw.bkt.clouddn.com/15233456161140.jpg" alt=""><br>把按钮拖到这个Exit上。选择上面的函数就好啦。</p>
<h1 id="JSON"><a href="#JSON" class="headerlink" title="JSON"></a>JSON</h1><h2 id="Encode-JSON"><a href="#Encode-JSON" class="headerlink" title="Encode JSON"></a>Encode JSON</h2><pre class="line-numbers language-swift"><code class="language-swift"> <span class="token keyword">func</span> <span class="token function">saveToJson</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token keyword">let</span> coder <span class="token operator">=</span> <span class="token function">JSONEncoder</span><span class="token punctuation">(</span><span class="token punctuation">)</span>

        <span class="token keyword">do</span> <span class="token punctuation">{</span>
            <span class="token keyword">let</span> data <span class="token operator">=</span> <span class="token keyword">try</span> coder<span class="token punctuation">.</span><span class="token function">encode</span><span class="token punctuation">(</span>weapons<span class="token punctuation">)</span>
            <span class="token keyword">let</span> saveUrl <span class="token operator">=</span> <span class="token function">URL</span><span class="token punctuation">(</span>fileURLWithPath<span class="token punctuation">:</span> <span class="token function">NSHomeDirectory</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">appendingPathComponent</span><span class="token punctuation">(</span><span class="token string">"weapons.json"</span><span class="token punctuation">)</span>

            <span class="token keyword">try</span> data<span class="token punctuation">.</span><span class="token function">write</span><span class="token punctuation">(</span>to<span class="token punctuation">:</span> saveUrl<span class="token punctuation">)</span>
            <span class="token function">print</span><span class="token punctuation">(</span><span class="token string">"保存成功！路径："</span><span class="token punctuation">,</span>saveUrl<span class="token punctuation">)</span>
        <span class="token punctuation">}</span> <span class="token keyword">catch</span> <span class="token punctuation">{</span>
            <span class="token function">print</span><span class="token punctuation">(</span><span class="token string">"编码错误: "</span><span class="token punctuation">,</span>error<span class="token punctuation">)</span>
        <span class="token punctuation">}</span>
    <span class="token punctuation">}</span>  
<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>
<h2 id="Decode-JSON"><a href="#Decode-JSON" class="headerlink" title="Decode JSON"></a>Decode JSON</h2><pre><code>func loadJson()  {
    let coder = JSONDecoder()

    do {
        let url = Bundle.main.url(forResource: &quot;weapons&quot;, withExtension: &quot;json&quot;)!
        let data = try Data(contentsOf: url)
        weapons = try coder.decode([Weapon].self, from: data)
    } catch  {
        print(error)
    }
}
</code></pre><h1 id="数据录入"><a href="#数据录入" class="headerlink" title="数据录入"></a>数据录入</h1><h2 id="Picker-View"><a href="#Picker-View" class="headerlink" title="Picker View"></a>Picker View</h2><ol>
<li>每个Picker加一个tag用来标识</li>
<li>ctrl拖到Controller上选上Delegate和DataSource</li>
<li>新建一个PickerViewFill.swift写入以下内容：</li>
</ol>
<pre class="line-numbers language-swift"><code class="language-swift"><span class="token keyword">import</span> <span class="token builtin">UIKit</span>

<span class="token keyword">extension</span> 你要扩展的面controller<span class="token punctuation">:</span><span class="token builtin">UIPickerViewDelegate</span><span class="token punctuation">,</span><span class="token builtin">UIPickerViewDataSource</span><span class="token punctuation">{</span>
<span class="token punctuation">}</span>
<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span></span></code></pre>
<p>会自动填充出两个函数。<br>一个控制行，一个控制列。</p>
<p>如何区分不同的列呢。<br><code>component</code>就是列数 （从0开始）</p>
<p>联动：打didselect联想出这个函数：</p>
<pre class="line-numbers language-swift"><code class="language-swift"><span class="token keyword">func</span> <span class="token function">pickerView</span><span class="token punctuation">(</span><span class="token number">_</span> pickerView<span class="token punctuation">:</span> <span class="token builtin">UIPickerView</span><span class="token punctuation">,</span> didSelectRow row<span class="token punctuation">:</span> <span class="token builtin">Int</span><span class="token punctuation">,</span> inComponent component<span class="token punctuation">:</span> <span class="token builtin">Int</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token keyword">if</span> pickerView<span class="token punctuation">.</span>tag <span class="token operator">==</span> <span class="token number">12</span> <span class="token punctuation">{</span>
            <span class="token keyword">if</span> component <span class="token operator">==</span> <span class="token number">0</span> <span class="token punctuation">{</span>
                pickerView<span class="token punctuation">.</span><span class="token function">reloadComponent</span><span class="token punctuation">(</span><span class="token number">1</span><span class="token punctuation">)</span>
            <span class="token punctuation">}</span>
        <span class="token punctuation">}</span>
    <span class="token punctuation">}</span>
<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>
<h2 id="Stepper"><a href="#Stepper" class="headerlink" title="Stepper"></a>Stepper</h2><pre class="line-numbers language-swift"><code class="language-swift"><span class="token atrule">@IBAction</span> <span class="token keyword">func</span> <span class="token function">mvStepper</span><span class="token punctuation">(</span><span class="token number">_</span> sender<span class="token punctuation">:</span> <span class="token builtin">UIStepper</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
        textFieldMv<span class="token punctuation">.</span>text <span class="token operator">=</span> <span class="token function">Int</span><span class="token punctuation">(</span>exactly<span class="token punctuation">:</span> sender<span class="token punctuation">.</span>value<span class="token punctuation">)</span><span class="token operator">!</span><span class="token punctuation">.</span>description
    <span class="token punctuation">}</span>
<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span></span></code></pre>
<h1 id="点击单元格"><a href="#点击单元格" class="headerlink" title="点击单元格"></a>点击单元格</h1><p><strong>didSelect</strong></p>
<pre class="line-numbers language-swift"><code class="language-swift"><span class="token keyword">override</span> <span class="token keyword">func</span> <span class="token function">tableView</span><span class="token punctuation">(</span><span class="token number">_</span> tableView<span class="token punctuation">:</span> <span class="token builtin">UITableView</span><span class="token punctuation">,</span> didSelectRowAt indexPath<span class="token punctuation">:</span> <span class="token builtin">IndexPath</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
<span class="token comment" spellcheck="true">// indexPath就是坐标 [0,0] 第一个是session号，第二个是行</span>
<span class="token punctuation">}</span>
<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span></span></code></pre>
<h2 id="Alert"><a href="#Alert" class="headerlink" title="Alert"></a>Alert</h2><h3 id="Alertsheet"><a href="#Alertsheet" class="headerlink" title="Alertsheet"></a>Alertsheet</h3><pre class="line-numbers language-swift"><code class="language-swift"> <span class="token keyword">let</span> actionSheet <span class="token operator">=</span> <span class="token function">UIAlertController</span><span class="token punctuation">(</span>title<span class="token punctuation">:</span> <span class="token string">"总标题"</span><span class="token punctuation">,</span> message<span class="token punctuation">:</span> <span class="token constant">nil</span><span class="token punctuation">,</span> preferredStyle<span class="token punctuation">:</span> <span class="token punctuation">.</span>actionSheet<span class="token punctuation">)</span>

            <span class="token keyword">let</span> action1 <span class="token operator">=</span> <span class="token function">UIAlertAction</span><span class="token punctuation">(</span>title<span class="token punctuation">:</span> <span class="token string">"111"</span><span class="token punctuation">,</span> style<span class="token punctuation">:</span> <span class="token punctuation">.</span><span class="token keyword">default</span> <span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token punctuation">(</span><span class="token number">_</span><span class="token punctuation">)</span> <span class="token keyword">in</span>
            <span class="token punctuation">}</span>
            <span class="token keyword">let</span> action2 <span class="token operator">=</span> <span class="token function">UIAlertAction</span><span class="token punctuation">(</span>title<span class="token punctuation">:</span> <span class="token string">"222"</span><span class="token punctuation">,</span> style<span class="token punctuation">:</span> <span class="token punctuation">.</span><span class="token keyword">default</span><span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token punctuation">(</span><span class="token number">_</span><span class="token punctuation">)</span> <span class="token keyword">in</span>

            <span class="token keyword">let</span> cancel <span class="token operator">=</span> <span class="token function">UIAlertAction</span><span class="token punctuation">(</span>title<span class="token punctuation">:</span> <span class="token string">"Cancel"</span><span class="token punctuation">,</span> style<span class="token punctuation">:</span> <span class="token punctuation">.</span>cancel<span class="token punctuation">,</span> handler<span class="token punctuation">:</span> <span class="token constant">nil</span><span class="token punctuation">)</span>

            actionSheet<span class="token punctuation">.</span><span class="token function">addAction</span><span class="token punctuation">(</span>action1<span class="token punctuation">)</span>
            actionSheet<span class="token punctuation">.</span><span class="token function">addAction</span><span class="token punctuation">(</span>action2<span class="token punctuation">)</span>

            <span class="token function">present</span><span class="token punctuation">(</span>actionSheet<span class="token punctuation">,</span>animated<span class="token punctuation">:</span> <span class="token boolean">true</span><span class="token punctuation">)</span>
<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>

            </div>
            <hr>

            

            <link rel="stylesheet" type="text/css" href="/libs/share/css/share.min.css">

<div id="article-share">
    
    <div class="social-share" data-disabled="qzone" data-wechat-qrcode-helper="<p>微信里点“发现”->“扫一扫”二维码便可查看分享。</p>"></div>
    
</div>

<script src="/libs/share/js/social-share.min.js"></script>

            <div class="reprint">
                <p>
                    <span class="reprint-tip">转载请注明: </span>
                    <a href="https://xuanshan.gitee.io" class="b-link-green">炫杉</a>
                    <i class="fa fa-angle-right fa-lg fa-fw text-color"></i>
                    <a href="/iOS/小波iOS11中级篇笔记/" class="b-link-green">小波iOS11中级篇笔记</a>
                </p>
            </div>
        </div>
    </div>

    

    

    

    
    <div class="livere-card card" data-aos="fade-up">
    <!-- 来必力City版安装代码 -->
    <div id="lv-container" class="card-content" data-id="city" data-uid="MTAyMC8yOTg2My82NDI5">
        <script type="text/javascript">
            (function (d, s) {
                let j, e = d.getElementsByTagName(s)[0];
                if (typeof LivereTower === 'function') {
                    return;
                }

                j = d.createElement(s);
                j.src = 'https://cdn-city.livere.com/js/embed.dist.js';
                j.async = true;

                e.parentNode.insertBefore(j, e);
            })(document, 'script');
        </script>
        <noscript>为正常使用来必力评论功能请激活JavaScript。</noscript>
    </div>
    <!-- City版安装代码已完成 -->
</div>
    

    

<article id="prenext-posts" class="prev-next articles">
    <div class="row article-row">
        
        <div class="article col s12 m6" data-aos="fade-up">
            <div class="article-badge left-badge text-color">上一篇</div>
            <div class="card">
                <a href="/iOS/UIDevice学习/">
                    <div class="card-image">
                        
                        
                        <img src="/medias/featureimages/12.jpg" class="responsive-img" alt="UIDevice学习">
                        
                        <span class="card-title">UIDevice学习</span>
                    </div>
                </a>
                <div class="card-content article-content">
                    <div class="summary">iOS的UIDevice可以方便获取设备的硬件和系统的一些状态信息。写这篇文章是因为选修了《移动安全与测评》这门课。小作业让写一个APP获取系统和其他App的一些信息。在不越狱的iOS中我实在不知道怎么获取其他APP的信息，就借此机会了解下</div>
                    <div class="publish-info">
                        <span class="publish-date">
                            <i class="fa fa-clock-o fa-fw icon-date"></i>2018-04-16
                        </span>
                        <span class="publish-author">
                            
                            <i class="fa fa-bookmark fa-fw icon-category"></i>
                            
                            <a href="/categories/iOS/" class="post-category" target="_blank">
                                    iOS
                                </a>
                            
                            
                        </span>
                    </div>
                </div>
                
                <div class="card-action article-tags">
                    
                    <a href="/tags/Swift/" target="_blank">
                        <span class="chip bg-color">Swift</span>
                    </a>
                    
                </div>
                
            </div>
        </div>
        
        
        <div class="article col s12 m6" data-aos="fade-up">
            <div class="article-badge right-badge text-color">下一篇</div>
            <div class="card">
                <a href="/iOS/SwiftDemo2/">
                    <div class="card-image">
                        
                        
                        <img src="/medias/featureimages/6.jpg" class="responsive-img" alt="Swift Demo2 查天气">
                        
                        <span class="card-title">Swift Demo2 查天气</span>
                    </div>
                </a>
                <div class="card-content article-content">
                    <div class="summary">通过API查天气。

载入界面简单，三个Label（地址、温度、描述）一个Btn。
先把Btn写出来。弹出Alert，附带一个文本输入框，把输入框的值传给通过Api获取天气数据的函数。代码如下：
@IBAction func setCity</div>
                    <div class="publish-info">
                            <span class="publish-date">
                                <i class="fa fa-clock-o fa-fw icon-date"></i>2018-04-02
                            </span>
                        <span class="publish-author">
                            
                            <i class="fa fa-user fa-fw"></i>
                            ixsim
                            
                        </span>
                    </div>
                </div>
                
            </div>
        </div>
        
    </div>
</article>
</div>
    </div>
    <div class="col l3 hide-on-med-and-down">
        <div class="toc-widget">
            <div class="toc-title">目录</div>
            <div id="toc-content">

            </div>
        </div>
    </div>
</div>

<script src="/libs/tocbot/tocbot.min.js"></script>
<script>
    $(function () {
        tocbot.init({
            tocSelector: '#toc-content',
            contentSelector: '#articleContent',
            headingSelector: 'h1,h2, h3, h4'
        });

        // modify the toc link href to support Chinese.
        let i = 0;
        let tocHeading = 'toc-heading-';
        $('#toc-content a').each(function () {
            $(this).attr('href', '#' + tocHeading + (++i));
        });

        // modify the heading title id to support Chinese.
        i = 0;
        $('#articleContent').children('h1,h2, h3, h4').each(function () {
            $(this).attr('id', tocHeading + (++i));
        });

        // Set scroll toc fixed.
        let tocHeight = parseInt($(window).height() * 0.4 - 64);
        let $tocWidget = $('.toc-widget');
        $(window).scroll(function () {
            let scroll = $(window).scrollTop();
            /* add post toc fixed. */
            if (scroll > tocHeight) {
                $tocWidget.addClass('toc-fixed');
            } else {
                $tocWidget.removeClass('toc-fixed');
            }
        });
    });
</script>
    

</main>

<footer class="page-footer bg-color">
    <div class="container row center-align">
        <div class="col s12 m8 l8 copy-right">
            &copy;<a href="https://o--o.win" target="_blank">炫杉2018</a> 
            <a href="https://hexo.io/" target="_blank">Hexo</a> 强力驱动
              主题 <a href="https://github.com/blinkfox/hexo-theme-matery" target="_blank">matery</a>        </div>
        <div class="col s12 m4 l4 social-link"><a href="https://github.com/ixsim" class="tooltipped" target="_blank" data-tooltip="访问我的GitHub" data-position="top" data-delay="50">
    <i class="fa fa-github"></i>
</a>

<a href="mailto:chenjiayin1990@163.com" class="tooltipped" target="_blank" data-tooltip="邮件:chinalixs@qq.com" data-position="top" data-delay="50">
    <i class="fa fa-envelope-open"></i>
</a>

<a href="http://wpa.qq.com/msgrd?v=3&uin=6956820&site=qq&menu=yes" class="tooltipped" data-tooltip="QQ: 6956820" data-position="top" data-delay="50">
    <i class="fa fa-qq"></i>
</a>
</div>
    </div>
</footer>

<div class="progress-bar"></div>


<!-- 搜索遮罩框 -->
<div id="searchModal" class="modal">
    <div class="modal-content">
        <div class="search-header">
            <span class="title">搜索</span>
            <input type="search" id="searchInput" name="s" placeholder="请输入搜索的关键字" class="search-input" autofocus>
        </div>
        <div id="searchResult"></div>
    </div>
</div>

<script src="/js/search.js"></script>
<script type="text/javascript">
    searchFunc("/" + "search.xml", 'searchInput', 'searchResult');
</script>
<!-- 回到顶部按钮 -->
<div id="backTop" class="top-scroll">
    <a class="btn-floating btn-large waves-effect waves-light" href="#!">
        <i class="fa fa-angle-up"></i>
    </a>
</div>


<script src="/libs/materialize/js/materialize.min.js"></script>
<script src="/libs/masonry/masonry.pkgd.min.js"></script>
<script src="/libs/aos/aos.js"></script>
<script src="/libs/scrollprogress/scrollProgress.min.js"></script>
<script src="/libs/lightGallery/js/lightgallery-all.min.js"></script>
<script src="/js/matery.js"></script>
</body>
</html>